-
-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Creates Test Plan for Running Integration Tests on Server API Endpoints #1493
Conversation
…into 1400-ag-accounts-api-tests
…into 1400-ag-accounts-api-tests
…into 1400-ag-accounts-api-tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added comments to files to explain
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OBSOLETE - This was a script to test using Hoppscotch
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
renamed file
# APPLICATIONINSIGHTS_CONNECTION_STRING= | ||
|
||
############################################################### | ||
## Testing (Comment out ALL the above variables) ## |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
renamed file with better naming convention and updated the .env file layout so that users can test locally by simply commenting out the development variables and un-commenting the testing variables.
I will have to add an updated .env file to the google drive upon accepted changes
await teardownServer(); | ||
}); | ||
|
||
beforeEach(() => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This mocks the sendgrid function call on the sendgrid client to avoid costs incurred from calling 3rd party API's
Documentation used to produce this sendgrid mock:
- I had to mock the sendgrid API because they dont have a test key. If I used their SandboxMode, it would only validate the data structure of the request so I wouldnt be able to intercept the request and grab the token for the email confirmation on the integration tests
References
- Sendgrid API docs
- This is where the 202 response code comes from
- Sendgrid mocked using jest
teardownServer | ||
} = require("../_jest-setup_/utils/server-setup"); | ||
|
||
let server; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the supertest library allows you to pass a real server instance. I chose to do this to mimic production more closely.
This is an optional pattern and you are able to avoid instantiating the server - supertest will use an in-memory representation. Simply remove the setupServer(); function call.
@@ -28,7 +28,8 @@ module.exports = function () { | |||
sqlMigrationSuffixes: ".sql", | |||
baselineOnMigrate: true, | |||
baselineVersion: "0002", // Do not change this baseline version number | |||
baselineDescription: "setup_db_baseline_data_as_of_07012020" | |||
baselineDescription: "setup_db_baseline_data_as_of_07012020", | |||
reportEnabled: process.env.TEST_ENV ? false : true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Running the flyway migrations creates a multi-thousand line report - this code prevents this from happening during testing but preserves this function during development and production environments
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
configurations for jest
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This adds supertest, jest, and testcontainers. These are popular libraries that are well maintained with stable dependencies.
@@ -9,7 +9,7 @@ | |||
"main": "server.js", | |||
"scripts": { | |||
"precommit": "lint-staged", | |||
"test": "jest --passWithNoTests", | |||
"test": "jest --runInBand --passWithNoTests --forceExit", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"--runInBand" is added to run the test files sequentially rather than in parallel because they are sharing a database instance.
"--forceExit" is a measure to force jest to exit in the case there is a server that is hanging up from being improperly closed.
@@ -75,6 +75,8 @@ app.use((err, req, res) => { | |||
|
|||
app.use(errorHandler); | |||
|
|||
app.listen(port, () => console.log(`Server running on port ${port}`)); | |||
if (process.env.TEST_ENV !== "true") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a minor conditional to be able to instantiate the server, i.e. set them up and tear them down. Development environments and production environments will have a falsy conditional; thus, be unaffected.
this was a necessary change to be able run multiple test suites with a real server.
I tried to keep commits atomic, but there was A LOT of refactoring; hence, the large amount of commits. We can squash and merge to condense this. |
Wiki is linked on comment above. Do review for further info and provide feedback as this is the "source of truth" to keep the test plan readable, maintainable, and scalable. |
@entrotech alternatively you can grab the env variables from the github actions file - just make sure all other variables in your .env are commented out! |
The below should facilitate interpretation of code Suggested Order for File Review (root directory would be server): Environment Variables Setting up a containerized db with application code Server refactors and setup for use as an instance on tests Testing configs and script Accounts tests Setting up github actions |
Fixes #1400
Overview of Added Features
What changes did you make?
Why did you make the changes (we will use this info to test)?
References
Jest
Supertest
Testcontainers
SendGrid (Mocked for Testing)
Flyway Migrations
Suggested Order for File Review (root directory would be server)
Environment Variables
0.
.example.env
Setting up a containerized db with application code
1a.
utils/mssql-container-setup.js
1b.
db/flyway-config.js
1c.
global-setup/global-setup.js
1d.
global-setup/global-teardown.js
Server refactors and setup for use as an instance on tests
2a.
server.js
2b.
utils/server-setup.js
2c.
local-setup/setup-after-env.js
Testing configs and script
3a.
jest.config.js
3b.
package.json
Accounts tests
4.
__tests__/account.test.js
Setting up github actions
5.
.github/workflows/tdm-server-tests.yml
Discovery Notes
WebAPI endpoints Testing Feature Requirements:
Considered Implementations: